package com.chuanyang.driverh.mainmy.fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.chuanyang.driverh.BaseApplication;
import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.base.BaseListFragment;
import com.chuanyang.driverh.mainmy.AddLinesActivity;
import com.chuanyang.driverh.responseresult.LinesManageData;
import com.chuanyang.driverh.responseresult.LinesManageResult;
import com.chuanyang.driverh.responseresult.Result;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class LineManageFragment extends BaseListFragment {
    private LinesManageAdapter myAdapter;
    private String data;
    public static LineManageFragment newInstance(String data) {
        LineManageFragment fragment = new LineManageFragment();
        Bundle bundle = new Bundle();
        bundle.putString("data", data);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = getArguments();
        if (bundle != null) {
            data = bundle.getString("data");
        }


    }
    @Override
    public BaseListAdapter createAdapter() {
        return myAdapter = new LinesManageAdapter(getActivity());
    }

    @Override
    public void onGetMore() {

        getLines();
    }

    @Override
    public void onRefresh() {
        mCurrentPage = 1;
        getLines();
    }
    @Override
    public void initView(View view) {
        super.initView(view);
        mlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                LinesManageData.DataBean bean = myAdapter.getItem(position);

                if (data.equals("1")) { //只去修改数据
                    startActivity(AddLinesActivity.class, bean);
                }
                else if (data.equals("2")) {
                    Intent intent = new Intent();
                    intent.putExtra("data", bean);
                    getActivity().setResult(2, intent);
                    getActivity().finish();
                }
            }
        });
    }

    @Override
    protected void initData() {

    }

    private void deleteLines(final LinesManageData.DataBean bean) {
        DialogUtil.showLoadingDialog(getActivity(),"删除线路...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("id", bean.getId() + "");
        MyHttpUtil.deleteLines(getActivity(),params, new MyCallback<Result>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    myAdapter.remove(bean);
                    PrintUtil.toast(getActivity(),"路线删除成功");
                    BaseApplication.getInstance().getDriverInfoFromNet();
                }else{
                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });


    }
    @Override
    public void onResume() {
        super.onResume();
        mCurrentPage = 1;
        getLines();
    }

    @Override
    protected void onForceRefresh() {
        mCurrentPage = 1;
        getLines();
    }

    private void getLines() {
        DialogUtil.showLoadingDialog(getActivity(),"线路...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("page", mCurrentPage + "");
        params.put("driverId", SharedPrefUtil.getUserId());
        MyHttpUtil.getLines(getActivity(),params, new MyCallback<LinesManageResult>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
                springView.onFinishFreshAndLoad();
                onShowFailed();
            }
            @Override
            public void onSuccess(LinesManageResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    setDataResult(result.getData().getData());
                }else{
                    springView.onFinishFreshAndLoad();
                    PrintUtil.toast(getActivity(),result.getMsg());
                    onShowNull(R.mipmap.bg1);
                }
            }
        });
    }



    @Override
    protected boolean isLoadMore() {
        return true;
    }

    @Override
    protected boolean isPullToRefresh() {
        return true;
    }

    @Override
    protected void initListener() {

    }


    private class LinesManageAdapter extends BaseListAdapter<LinesManageData.DataBean> {

        private ViewHolder holder;

        public LinesManageAdapter(Context context) {
            super(context);//此处不传入集合，则默认集合长度为0，在setdataresult（）方法中，添加数据并刷新
        }

        @Override
        public View getZkView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = View.inflate(getActivity(), R.layout.my_item_linesmanage, null);
                holder = new ViewHolder();
                holder.iv_lines_delete = (ImageView) convertView.findViewById(R.id.iv_lines_delete);
                // holder.iv_lines_startcity= (ImageView) convertView.findViewById(R.id.iv_lines_startcity);
                // holder.iv_lines_endcity= (ImageView) convertView.findViewById(R.id.iv_lines_endcity);
                holder.tv_path_manage_start_city = (TextView) convertView.findViewById(R.id.tv_path_manage_start_city);
                holder.tv_path_manage_end_city = (TextView) convertView.findViewById(R.id.tv_path_manage_end_city);
                holder.tv_path_manage_start_city = (TextView) convertView.findViewById(R.id.tv_path_manage_start_city);
                holder.tv_lines_startcity = (TextView) convertView.findViewById(R.id.tv_lines_startcity);
                holder.tv_lines_endcity = (TextView) convertView.findViewById(R.id.tv_lines_endcity);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            LinesManageData.DataBean bean = getItem(position);
            holder.tv_path_manage_start_city.setText(bean.getStartCity());
            holder.tv_lines_startcity.setText(bean.getStartAddr());
            holder.tv_path_manage_end_city.setText(bean.getTargetCity());
            holder.tv_lines_endcity.setText(bean.getTargetAddr());
            holder.iv_lines_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    deleteLines(LinesManageAdapter.this.getItem(position));
                }
            });
            return convertView;
        }
    }

    private class ViewHolder {
        ImageView iv_lines_endcity;
        ImageView iv_lines_startcity;
        ImageView iv_lines_delete;
        TextView tv_path_manage_start_city;//起点城市
        TextView tv_path_manage_end_city;
        TextView tv_lines_startcity;//详细起点
        TextView tv_lines_endcity;
    }
}
